<?php
	include("HTML_top.php");
	
	if (isset($_GET['logout']))
	{
		session_destroy();
		echo '<script type="text/javascript">window.location="index.php";</script>';
	}
	
	include("header.php");
?>

<?php

	if (isset($_POST['new_email']))
	{
		include("conn.php");
		connectToDB();
		
		mysql_query("INSERT INTO user VALUES ('".$_POST['new_email']."', '".$_POST['new_name']."', '".$_POST['new_pin']."')");
	}

	if (isset($_SESSION['user']))
	{
		include("conn.php");
		connectToDB();
		
		if (isset($_POST['createCB_title']))
		{
			$q = "INSERT INTO corkboard VALUES (";
			
			$q .= "'" . $_SESSION['user']['email'] . "', ";
			$q .= "'" . addslashes($_POST['createCB_title']) . "', ";
			$q .= "'" . addslashes($_POST['createCB_cat']) . "', ";
			
			if ($_POST['createCB_vis'] == 'Public')
				$q .= 'NULL, 0);';
			else
				$q .= "'" . addslashes($_POST['createCB_pass']) . "', 1);";
			
			if (!mysql_query($q))
				echo '<font style="color:red">Could not create CorkBoard -- duplicate title exists!</font><br/><br/>';
		}
		
		if (isset($_GET['del']))
		{
			mysql_query("DELETE FROM corkboard WHERE email='".$_SESSION['user']['email']."' AND title='".addslashes($_GET['del'])."';");
		}
		
		if (isset($_GET['follow']))
		{
			mysql_query("INSERT INTO user_follows VALUES('".$_SESSION['user']['email']."', '".$_GET['follow']."');");
			echo '<font style="color:green">You are now following '.$_GET['name'].'</font><br/><br/>';
		}
		
		if (isset($_GET['unfollow']))
		{
			mysql_query("DELETE FROM user_follows WHERE follower='".$_SESSION['user']['email']."' AND followed='".$_GET['unfollow']."';");
			echo '<font style="color:green">You are no longer following '.$_GET['name'].'</font><br/><br/>';
		}
	
		echo '<b>' . $_SESSION['user']['name'] . "'s home page</b><hr/>";
		echo '<a href="popular.php">View popular tags</a>';
		echo '<h2>Recent CorkBoard updates</h2>';
		
		$q = mysql_query("

		SELECT `A`.email, `A`.title, `A`.name, MAX(`modified`), `private` FROM
		(

		-- cb’s user owns
		SELECT `corkboard`.email, `title`, `name`
		FROM `corkboard`
		INNER JOIN `user` ON `corkboard`.email=`user`.email
		WHERE `corkboard`.email='".$_SESSION['user']['email']."'

		UNION

		-- cb’s user watching
		SELECT `user_watches`.email, `title`, `name`
		FROM `user_watches`
		INNER JOIN `user` ON `user_watches`.email=`user`.email
		WHERE `watcher`='".$_SESSION['user']['email']."'

		UNION

		-- cb’s user following
		SELECT `corkboard`.email, `title`, `name`
		FROM `corkboard`
		INNER JOIN `user` ON `corkboard`.email=`user`.email
		WHERE `corkboard`.email IN
		(
		SELECT `followed`
		FROM `user_follows`
		WHERE `follower`='".$_SESSION['user']['email']."'
		)

		) AS `A`

		-- get visibility (private) info
		INNER JOIN `corkboard` ON `A`.email=`corkboard`.email AND `A`.title=`corkboard`.title

		-- get last modified info
		INNER JOIN `pushpin` ON `A`.email=`pushpin`.email AND `A`.title=`pushpin`.title

		GROUP BY `A`.email, `A`.title
		ORDER BY MAX(`modified`) DESC
		LIMIT 0, 4;
		
		");
		
		if (mysql_num_rows($q) == 0)
			echo 'No updates';
		else
		{
			while ($r = mysql_fetch_array($q))
			{
				echo '<div class="recentCB">';
			
				echo '<a href="corkboard.php?email=' . urlencode($r['email']) . '&title=' . urlencode($r['title']) . '" style="font-weight:bold">' . $r['title'] . '</a>';
				if ($r['private'] == '1')
					echo ' (private)';
				echo '<br/>';
				echo 'Updated by <b>' . $r['name'] . '</b> on <b>' . date('g:i A F j, Y', strtotime($r["MAX(`modified`)"])) . '</b>';
			
				echo '</div>';
		
			}
		}
		
		echo '<h2>My CorkBoards</h2>';
		
		$q = mysql_query("

		SELECT c.title, c.private, COUNT(p.url)
		FROM corkboard AS c
		LEFT JOIN pushpin AS p
		ON c.email = p.email
		AND c.title = p.title
		WHERE c.email = '".$_SESSION['user']['email']."'
		GROUP BY c.title, c.private
		ORDER BY c.title ASC;
		
		");
		
		echo '<div class="recentCB">';
		if (mysql_num_rows($q) == 0)
			echo 'You have no CorkBoards';
		else
		{
			while ($r = mysql_fetch_array($q))
			{
				echo '<a href="corkboard.php?email=' . urlencode($_SESSION['user']['email']) . '&title=' . urlencode($r['title']) . '" style="font-weight:bold">' . $r['title'] . '</a>';
			
				if ($r['private'] == '1')
					echo ' (private)';
			
				echo ' with ' . $r['COUNT(p.url)'] . ' PushPins';
				echo '<br/><br/>';
		
			}
		}
		echo '<hr/><a href="create_corkboard.php">Create new CorkBoard</a>';
		echo '</div>';
		
		echo '<hr/>';
		
		echo '<form action="search.php" method="get">';
		echo 'Search description, tags, and CorkBoard category:<br/>';
		echo '<input type="text" name="q" size="50" />&nbsp;';
		echo '<input type="submit" value="PushPin Search" />';
		echo '</form>';
		
	}
	else
	{
		echo '<b>welcome to corkboardit!</b><p>corkboardit is a bulletin board where users post and share photos in a virtual community. To begin, please login to your account or <a href="register.php">register a new account</a></p>';
		
	}

?>

<?php
	include("footer.php");
	include("HTML_bottom.php");
?>
